/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InputDosen.java
 *
 * Created on Oct 19, 2010, 7:50:44 PM
 */
package stta.bahrie.admin.gui;

import api.stta.bahrie.entiy.Dosen;
import api.stta.bahrie.inter.DosenInterf;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author bahrie
 */
public class InputDosen extends javax.swing.JInternalFrame {

    DosenInterf dsnService;
    List<Dosen> records = new ArrayList<Dosen>();
    int row;

    /** Creates new form DataDosen */
    public InputDosen(DosenInterf dsnService) {
        try {
            this.dsnService = dsnService;
            initComponents();
            this.statusAwal();
            tableDosen.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

                public void valueChanged(ListSelectionEvent e) {
                    row = tableDosen.getSelectedRow();
                    if (row != -1) {
                        isiTeks();
                    }
                }
            });
        } catch (RemoteException ex) {
            Logger.getLogger(InputDosen.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    void kosongkanTeks() {
        txtDosen.setText("");
        txtKdDosen.setText("");
        txtPwd.setText("");
    }

    void isiTeks() {
        Dosen dsn = records.get(row);
        txtKdDosen.setText(dsn.getKd_dosen());
        txtDosen.setText(dsn.getDosen());
        txtPwd.setText(dsn.getPwd());
    }

    void isiTabel() {
        try {
            Object[][] data = new Object[records.size()][3];

            int x = 0;
            for (Dosen dsn : records) {
                data[x][0] = dsn.getKd_dosen();
                data[x][1] = dsn.getDosen();
                data[x][2] = dsn.getPwd();
                x++;
            }
            String[] judul = {"Kode Dosen", "Nama"};
            tableDosen.setModel(new DefaultTableModel(data, judul));
            jScrollPane1.setViewportView(tableDosen);
        } catch (Throwable ex) {
            ex.printStackTrace();
        }
    }

    void loadRecords() throws RemoteException {
        records = dsnService.getAll();
        isiTabel();
    }

    void statusAwal() throws RemoteException {
        this.loadRecords();
        this.kosongkanTeks();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtKdDosen = new javax.swing.JTextField();
        txtDosen = new javax.swing.JTextField();
        txtPwd = new javax.swing.JPasswordField();
        jPanel3 = new javax.swing.JPanel();
        inputButton = new javax.swing.JButton();
        hapusButton = new javax.swing.JButton();
        updateBotton = new javax.swing.JButton();
        resetBotton = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableDosen = new javax.swing.JTable();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Input Dosen");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Input Dosen"));

        jLabel1.setText(" Kode Dosen");

        jLabel2.setText("Nama :");

        jLabel5.setText("Password :");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtKdDosen, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
                    .addComponent(txtDosen, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
                    .addComponent(txtPwd, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtKdDosen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtDosen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtPwd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        inputButton.setText("Input");
        inputButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                inputButtonActionPerformed(evt);
            }
        });
        jPanel3.add(inputButton);

        hapusButton.setText("Hapus");
        hapusButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hapusButtonActionPerformed(evt);
            }
        });
        jPanel3.add(hapusButton);

        updateBotton.setText("Update");
        updateBotton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateBottonActionPerformed(evt);
            }
        });
        jPanel3.add(updateBotton);

        resetBotton.setText("Reset");
        resetBotton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetBottonActionPerformed(evt);
            }
        });
        jPanel3.add(resetBotton);

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Tabel Dosen"));

        tableDosen.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tableDosen);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void inputButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inputButtonActionPerformed
        try {
            // TODO add your handling code here:
            Dosen dsn = new Dosen();
            dsn.setKd_dosen(txtKdDosen.getText());
            dsn.setDosen(txtDosen.getText());
            dsn.setPwd(txtPwd.getText());
            dsnService.insert(dsn);
            this.statusAwal();
        } catch (RemoteException ex) {
            Logger.getLogger(InputDosen.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_inputButtonActionPerformed

    private void hapusButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hapusButtonActionPerformed
        try {
            // TODO add your handling code here:
            String kode = records.get(tableDosen.getSelectedRow()).getKd_dosen();
            dsnService.delete(kode);
            this.statusAwal();
        } catch (RemoteException ex) {
            Logger.getLogger(InputDosen.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_hapusButtonActionPerformed

    private void updateBottonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateBottonActionPerformed
        try {
            // TODO add your handling code here:
            Dosen dsn = records.get(tableDosen.getSelectedRow());
            dsn.setKd_dosen(txtKdDosen.getText());
            dsn.setDosen(txtDosen.getText());
            dsn.setPwd(txtPwd.getText());
            dsnService.update(dsn);
            this.statusAwal();
        } catch (RemoteException ex) {
            Logger.getLogger(InputDosen.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_updateBottonActionPerformed

    private void resetBottonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetBottonActionPerformed
        try {
            // TODO add your handling code here:
            this.statusAwal();
        } catch (RemoteException ex) {
            Logger.getLogger(InputDosen.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_resetBottonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton hapusButton;
    private javax.swing.JButton inputButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton resetBotton;
    private javax.swing.JTable tableDosen;
    private javax.swing.JTextField txtDosen;
    private javax.swing.JTextField txtKdDosen;
    private javax.swing.JPasswordField txtPwd;
    private javax.swing.JButton updateBotton;
    // End of variables declaration//GEN-END:variables
}
